﻿
Imports std
Imports storoomc

Namespace data
    Public Class category
        Inherits list

        Public Const _keyprefix As String = "cat"

        Protected Overrides Function enableModify() As Boolean
            Return False
        End Function

        Protected Overrides Function enableRead() As Boolean
            Return True
        End Function

        Protected Overrides Function enableUniqueWrite() As Boolean
            Return False
        End Function

        Protected Overrides Function enableWrite() As Boolean
            Return True
        End Function

        Protected Overrides Function keyprefix() As String
            Return _keyprefix
        End Function
    End Class

    Public Class record
        Inherits list

        Public Const _keyprefix As String = "rec"

        Public Overrides Function enableCache() As Boolean
            Return Not isdebugmode()
        End Function

        Protected Overrides Function enableModify() As Boolean
            Return False
        End Function

        Protected Overrides Function enableRead() As Boolean
            Return True
        End Function

        Protected Overrides Function enableUniqueWrite() As Boolean
            Return False
        End Function

        Protected Overrides Function enableWrite() As Boolean
            Return True
        End Function

        Protected Overrides Function keyprefix() As String
            Return _keyprefix
        End Function

        Private Function delete(Optional ByVal storoom As storoomTalk = Nothing) As Boolean
            If storoom Is Nothing Then
                storoom = _storoom.storoom
            End If
            Dim info As info = Nothing
            info = New info()
            Dim key As String = Nothing
            Dim rtn As Boolean = True
            Dim i As Int64
            For i = 0 To size() - 1
                key = storehelper.generatekey(info, at(i))
                If isdebugmode() Then
                    raiseError("delete info " + key)
                End If
                If Not storoom.delete(key) Then
                    raiseError("cannot delete info " + key, errorHandle.errorType.warning)
                    rtn = False
                End If
            Next

            Return rtn
        End Function

        Public Shared Function delete(ByVal key As String, Optional ByVal storoom As storoomTalk = Nothing) As Boolean
            If storoom Is Nothing Then
                storoom = _storoom.storoom
            End If
            Dim rec As record = Nothing
            rec = New record()
            If rec.read(key, storoom) Then
                rec.delete(storoom)
                If Not storoom.delete(storehelper.generatekey(rec, key)) Then
                    raiseError("cannot delete record " + key, errorHandle.errorType.warning)
                    Return False
                Else
                    Return True
                End If
            Else
                raiseError("cannot read record " + key, errorHandle.errorType.warning)
                Return False
            End If
        End Function
    End Class

    'do not need to sort inside info/list, just sort them in record/list
    Public Class info
        Inherits list

        Public Const _keyprefix As String = "inf"
        Private _text As String = Nothing

        Public Overrides Function enableCache() As Boolean
            Return Not isdebugmode()
        End Function

        Public Enum index
            time = 0
            [from] = 1
            id = 2
            text = 3

            count = text + 1
        End Enum

        Public Overrides Sub clear()
            MyBase.clear()
            _text = Nothing
        End Sub

        Protected Overrides Function enableEmptyEntity() As Boolean
            Return True
        End Function

        Protected Overrides Function enableModify() As Boolean
            Return False
        End Function

        Protected Overrides Function enableRead() As Boolean
            Return True
        End Function

        Protected Overrides Function enableUniqueWrite() As Boolean
            Return True
        End Function

        Protected Overrides Function enableWrite() As Boolean
            Return False
        End Function

        Protected Overrides Function keyprefix() As String
            Return _keyprefix
        End Function

        Private Sub fill()
            While size() < index.count
                push_back(Nothing)
            End While
        End Sub

        Public Property time() As String
            Get
                Return at(index.time)
            End Get
            Set(ByVal value As String)
                fill()
                at(index.time) = value
            End Set
        End Property

        Public Property [from]() As String
            Get
                Return at(index.from)
            End Get
            Set(ByVal value As String)
                fill()
                at(index.from) = value
            End Set
        End Property

        Public Property id() As String
            Get
                Return at(index.id)
            End Get
            Set(ByVal value As String)
                fill()
                at(index.id) = value
            End Set
        End Property

        Public Property text() As String
            Get
                If _text Is Nothing Then
                    _text = others(index.text)
                End If
                Return _text
            End Get
            Set(ByVal value As String)
                fill()
                at(index.text) = value
            End Set
        End Property

        Public Shared Function generate(ByVal context As httpContext) As info
            assert(Not context Is Nothing, "should not generate with an invalid context.")
            Dim info As info = Nothing
            info = New info()
            info.time = longTime()
            info.from = context.requestIp()
            Dim s As String = Nothing
            If getIdFromContext(context, s) Then
                copy(info.id, s)
            End If
            Dim it As map(Of String, String).iterator = Nothing
            it = context.url().query.find(query.text)
            If it <> context.url().query.end() Then
                info.text = (+it).second
            End If

            Return info
        End Function
    End Class

    Namespace alllist
        Public MustInherit Class list
            Inherits storoomc.list

            Public Const key As String = "all"

            Protected Overrides Function enableModify() As Boolean
                Return True
            End Function

            Protected Overrides Function enableRead() As Boolean
                Return True
            End Function

            Protected Overrides Function enableUniqueWrite() As Boolean
                Return False
            End Function

            'avoid duplicate
            Protected Overrides Function enableWrite() As Boolean
                Return False
            End Function

            Public Shadows Function read(Optional ByVal storoom As storoomTalk = Nothing) As Boolean
                Return MyBase.read(key, storoom)
            End Function

            Public Shadows Function modify(Optional ByVal storoom As storoomTalk = Nothing) As Boolean
                Return MyBase.modify(key, storoom)
            End Function
        End Class

        Public Class category
            Inherits list

            Public Const _keyprefix As String = "catal"

            Protected Overrides Function keyprefix() As String
                Return _keyprefix
            End Function
        End Class

        Public Class record
            Inherits list

            Public Const _keyprefix As String = "recal"

            Protected Overrides Function keyprefix() As String
                Return _keyprefix
            End Function
        End Class

        Public Class info
            Inherits list

            Public Const _keyprefix As String = "infal"

            Protected Overrides Function keyprefix() As String
                Return _keyprefix
            End Function
        End Class
    End Namespace

    Namespace timeline
        'store existing index list for a special entity <user / topic / category>
        Public MustInherit Class list
            Inherits storoomc.list

            Protected Overrides Function enableModify() As Boolean
                Return True
            End Function

            Protected Overrides Function enableRead() As Boolean
                Return True
            End Function

            Protected Overrides Function enableUniqueWrite() As Boolean
                Return False
            End Function

            Protected Overrides Function enableWrite() As Boolean
                Return False
            End Function
        End Class

        Public Class userlist
            Inherits list

            Public Const _keyprefix As String = "usrtll"

            Protected Overrides Function keyprefix() As String
                Return _keyprefix
            End Function
        End Class

        Public Class recordlist
            Inherits list

            Public Const _keyprefix As String = "rectll"

            Protected Overrides Function keyprefix() As String
                Return _keyprefix
            End Function
        End Class

        Public Class categorylist
            Inherits list

            Public Const _keyprefix As String = "cattll"

            Protected Overrides Function keyprefix() As String
                Return _keyprefix
            End Function
        End Class

        'store all related info/topic before a certain time
        Public MustInherit Class index
            Inherits storoomc.list

            Public Const dateOffset As Int64 = 0
            Public Const weekOffset As Int64 = 1
            Public Const monthOffset As Int64 = 2
            Public Const yearOffset As Int64 = 3
            Public Const offsetWidth As Int64 = 10
            Public Const timeTickNameSeparator As Char = character.underline

            Public Shared Function thisdaytick(ByVal tick As Int64) As Int64
                Return thisday(tick) + dateOffset
            End Function

            Public Shared Function isdaytick(ByVal tick As Int64) As Boolean
                Return ((tick - dateOffset) Mod offsetWidth) = 0
            End Function

            Public Shared Function thisweektick(ByVal tick As Int64) As Int64
                Return thisweek(tick) + weekOffset
            End Function

            Public Shared Function isweektick(ByVal tick As Int64) As Boolean
                Return ((tick - weekOffset) Mod offsetWidth) = 0
            End Function

            Public Shared Function thismonthtick(ByVal tick As Int64) As Int64
                Return thismonth(tick) + monthOffset
            End Function

            Public Shared Function ismonthtick(ByVal tick As Int64) As Boolean
                Return ((tick - monthOffset) Mod offsetWidth) = 0
            End Function

            Public Shared Function thisyeartick(ByVal tick As Int64) As Int64
                Return thisyear(tick) + yearOffset
            End Function

            Public Shared Function isyeartick(ByVal tick As Int64) As Boolean
                Return ((tick - yearOffset) Mod offsetWidth) = 0
            End Function

            Protected Overrides Function enableModify() As Boolean
                Return True
            End Function

            Protected Overrides Function enableRead() As Boolean
                Return True
            End Function

            Protected Overrides Function enableUniqueWrite() As Boolean
                Return False
            End Function

            Protected Overrides Function enableWrite() As Boolean
                Return False
            End Function

            Public Shared Function generatekey(ByVal name As String, ByVal timeTick As Int64) As String
                Return Convert.ToString(timeTick) + timeTickNameSeparator + name
            End Function

            Public Shared Function breakupkey(ByVal key As String, _
                                              ByRef name As String, ByRef timetick As Int64) As Boolean
                Dim timetickStr As String = Nothing
                If strseg(key, timetickStr, name, timeTickNameSeparator, True) Then
                    Return convertor.toint64(timetickStr, timetick, frombase:=10)
                Else
                    Return False
                End If
            End Function

            Public Shadows Function write(ByVal name As String, ByVal timetick As Int64, _
                                          Optional ByVal talk As storoomTalk = Nothing) As Boolean
                Return MyBase.write(generatekey(name, timetick), storoom)
            End Function

            Public Shadows Function read(ByVal name As String, ByVal timetick As Int64, _
                                         Optional ByVal talk As storoomTalk = Nothing) As Boolean
                Return MyBase.read(generatekey(name, timetick), storoom)
            End Function
        End Class

        Public Class userindex
            Inherits index

            Public Const _keyprefix As String = "usrtli"

            Protected Overrides Function keyprefix() As String
                Return _keyprefix
            End Function
        End Class

        Public Class recordindex
            Inherits index

            Public Const _keyprefix As String = "rectli"

            Protected Overrides Function keyprefix() As String
                Return _keyprefix
            End Function
        End Class

        Public Class categoryindex
            Inherits index

            Public Const _keyprefix As String = "cattli"

            Protected Overrides Function keyprefix() As String
                Return _keyprefix
            End Function
        End Class
    End Namespace
End Namespace